package org.lora.webframework.sqlexcute;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.lora.core.annotation.Action;
import org.lora.core.configxml.entity.MyBatisConfig;
import org.lora.core.context.WebContext;
import org.lora.core.mybatis.SqlExcutePlugin;
import org.lora.mvc.annotation.RequestPath;
import org.lora.mvc.annotation.ResponseJSON;
import org.lora.mvc.entity.ViewData;
import org.lora.webframework.common.WebframeBaseAction;

/**
 * <p>
 * 类名:SqlExcuteAction
 * </p>
 * 描述:sql执行Action<br>
 * 创建时间:2016年7月25日 下午3:06:25<br>
 * 
 * @author Bladnir@outlook.com<br>
 *         修改历史:<br>
 *         2016年7月25日 Bladnir 创建<br>
 */
@Action
@RequestPath(path = "/webframework/sqlExcute")
public class SqlExcuteAction extends WebframeBaseAction {

	/**
	 * 打开sql执行页面
	 * 
	 * @return
	 */
	@RequestPath(path = "/showSqlExcute")
	public ViewData showSqlExcute() {
		return new ViewData("/jsp/webframework/sqlexcute/sqlExcute.jsp");
	}

	@RequestPath(path = "/getPageData")
	@ResponseJSON
	public Object getPageData() {
		List<MyBatisConfig> mybatislist = WebContext.getInstance().getConfigXmlEntity().getMyBatisConfigList();

		List<Map<String, Object>> list = new ArrayList<>();

		for (MyBatisConfig myBatisConfig : mybatislist) {
			Map<String, Object> map = new HashMap<>();
			map.put("value", myBatisConfig.getAnnotationClassName());
			list.add(map);
		}

		return list;

	}

	/**
	 * 执行SQL语句
	 * 
	 * @param request
	 * @return
	 */
	@RequestPath(path = "/excuteSql")
	@ResponseJSON
	public Object excuteSql(HttpServletRequest request) {

		Map<String, Object> map = new HashMap<>();

		String sql = request.getParameter("sql");
		String annotationDAO = request.getParameter("database");

		Map<String, SqlSessionFactory> sqlSessionFactoryMap = WebContext.getInstance().getSqlSessionFactoryMap();

		SqlSessionFactory sqlSessionFactory = sqlSessionFactoryMap.get(annotationDAO);

		if (sqlSessionFactory == null) {
			return map;
		}

		SqlSession sqlSession = null;

		try {
			sqlSession = sqlSessionFactory.openSession();
			SqlExcutePlugin sqlMapper = new SqlExcutePlugin(sqlSession);

			Object data = sqlMapper.sqlExcute(sql);

			if (data == null) {
				List<Map<String, Object>> list = new ArrayList<>();
				Map<String, Object> rsmap = new HashMap<>();
				rsmap.put("result", "未知错误 SQL=[" + sql + "]");

				list.add(rsmap);
				map.put("tableData", list);
			} else if (Integer.class.isAssignableFrom(data.getClass())) {
				List<Map<String, Object>> list = new ArrayList<>();
				Map<String, Object> rsmap = new HashMap<>();
				rsmap.put("result", data);

				list.add(rsmap);
				map.put("tableData", list);
			} else {
				map.put("tableData", data);
			}

			sqlSession.commit();
		} catch (Exception e) {
			super.systemLog.error(e.getMessage(), e);

			List<Map<String, Object>> list = new ArrayList<>();
			Map<String, Object> rsmap = new HashMap<>();
			rsmap.put("result", e.getMessage());

			list.add(rsmap);
			map.put("tableData", list);

		} finally {
			if (sqlSession != null) {
				sqlSession.close();
			}
		}

		return map;

	}

}
